#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
#define maxn 1000
//给定一个二叉树的前序和后序序列,给出可能的中序序列的个数
char preOrder[maxn];
char postOrder[maxn];
long countNum(int preL, int preR, int postL, int postR)
{
    if (preR <= preL)
        return 1;
    char root = preOrder[preL]; //根节点
    if (preOrder[preL + 1] != postOrder[postR - 1])
    {
        //二叉树存在左右分支,可能出现的结果就是左分支的可能数*右分支的可能数
        char l = preOrder[preL + 1];
        char r = postOrder[postR - 1];
        int prer = preL, postl = postR;
        while (preOrder[prer] != r)
            prer++;
        while (postOrder[postl] != l)
            postl--;
        return countNum(preL + 1, prer - 1, postL, postl) *
               countNum(prer, preR, postl + 1, postR - 1);
    }
    else //二叉树有一支为空,另外一支非空,总数x2表示两种可能都考虑
    {
        return 2 * countNum(preL + 1, preR, postL, postR - 1);
    }
}
int main()
{
    scanf("%s%s", preOrder, postOrder);
    int len = strlen(preOrder);
    printf("%ld", countNum(0, len - 1, 0, len - 1));
    return 0;
}